﻿#include <QDir>
#include <Resources.h>
#include "FileSystemTreeWidget.h"
#include "FileSystemTreeWidgetItem.h"

namespace Domy {

/**
 * @brief FileSystemItem
 */
QString FileSystemItem::getText() const
{
    return this->text(0);
}

void FileSystemItem::setFileInfo(const QFileInfo &fi)
{
    m_fi = fi;

    updateDisplay();
}

QFileInfo FileSystemItem::getFileInfo() const
{
    return m_fi;
}

void FileSystemItem::clearChildren()
{
    QList<QTreeWidgetItem *> children = this->takeChildren();
    for (QTreeWidgetItem * sub_item : children) {
        delete sub_item;
        sub_item = Q_NULLPTR;
    }
}

/**
 * @brief DirItem
 */
DirItem::DirItem(const FileSystemDisplayProvider * const provider, bool create_temp)
    : FileSystemItem(provider, ItemType_Dir)
{
    if (create_temp) {
        // 增加一个空节点，使当前节点为可展开/折叠状态
        addTempEmptyItem();
    }
}

QString DirItem::getName() const
{
    QString name;
    if (m_fi.isRoot()) {
        name = m_fi.filePath();
    }
    else {
        name = m_fi.fileName();
    }
    return name;
}

void DirItem::expand()
{
    if (!this->treeWidget()) {
        return;
    }

    QDir dir(m_fi.absoluteFilePath());
    if (!dir.exists()) {
        return;
    }

    this->clearChildren();

    QFileInfoList fi_list = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name | QDir::DirsFirst);

    if (fi_list.isEmpty()) {
        // 增加一个空节点，使当前节点为可展开/折叠状态
        addTempEmptyItem();
    }
    else {
        for (const QFileInfo &fi : fi_list) {
            FileSystemItem *sub_item = Q_NULLPTR;
            if (fi.isDir()) {
                sub_item = new DirItem(m_provider);
            }
            else {
                sub_item = new FileItem(m_provider);
            }
            sub_item->setFileInfo(fi);

            this->addChild(sub_item);
        }
    }

    this->treeWidget()->expandItem(this);

    updateDisplay();
}

void DirItem::collapse()
{
    if (!this->treeWidget()) {
        return;
    }

    this->clearChildren();

    // 增加一个空节点，使当前节点为可展开/折叠状态
    addTempEmptyItem();

    updateDisplay();

    this->treeWidget()->collapseItem(this);
}

bool DirItem::isEmpty() const
{
    QDir dir(m_fi.absoluteFilePath());
    if (!dir.exists()) {
        return true;
    }

    QFileInfoList fi_list = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDir::Name | QDir::DirsFirst);

    return fi_list.isEmpty();
}

void DirItem::updateDisplay()
{
    auto display = m_provider->getDisplayInfo(m_fi, this->isExpanded() ? 1 : 0);

    this->setIcon(0, display.second);

    if (this->text(0) != display.first) {
        this->setText(0, display.first);
    }

    if (this->toolTip(0) != m_fi.absoluteFilePath()) {
        this->setToolTip(0, m_fi.absoluteFilePath());
    }
}

void DirItem::addTempEmptyItem()
{
    // 增加一个空节点，使当前节点为可展开状态
    // 节点展开后，将读取当前节点下的文件夹和文件
    // 节点关闭后，清空并增加一个空节点，使其为可展开状态
    // 同时将空节点置为disabled状态，以及标识为临时空节点
    QTreeWidgetItem *empty_item = new QTreeWidgetItem(FileSystemItem::ItemType_Temp);
    empty_item->setDisabled(true);
    this->addChild(empty_item);
}

/**
 * @brief FileItem
 */
QString FileItem::getName() const
{
    return m_fi.fileName();
}

void FileItem::updateDisplay()
{
    auto display = m_provider->getDisplayInfo(m_fi);

    this->setIcon(0, display.second);

    if (this->text(0) != display.first) {
        this->setText(0, display.first);
    }

    if (this->toolTip(0) != m_fi.absoluteFilePath()) {
        this->setToolTip(0, m_fi.absoluteFilePath());
    }
}

} // namespace Domy
